import * as Cesium from "cesium";
export class MixedLayer {
  constructor(viewer = window.cesiumViewer) {
    const dataSource = new Cesium.CustomDataSource("MixedLayer");
    viewer.dataSources.add(dataSource);
    Object.assign(this, {
      viewer,
      dataSource,
      domEntities: [],
      primitiveCollection: viewer.scene.primitives.add(
        new Cesium.PrimitiveCollection(),
      ),
      destroyCallback: [],
    });
  }

  destroy() {
    this.viewer.dataSources.remove(this.dataSource, true);
    this.domEntities.forEach((dom) => dom.destroy());

    this.viewer.scene.primitives.remove(this.primitiveCollection);
    this.primitiveCollection.destroy();

    Object.values(this.destroyCallback).forEach((callback) => callback());
  }
  toggleVisibility(isVisible) {
    // this.dataSource.entities.values.forEach((entity) => {
    //   entity.show = isVisible;
    // });
    this.dataSource.show = isVisible;
    this.domEntities.forEach((domElement) => {
      domElement.show = isVisible;
    });
    const { primitiveCollection } = this;
    for (let i = 0; i < primitiveCollection.length; i++) {
      primitiveCollection.get(i).show = isVisible;
    }
  }
}
